package com.czf.alibaba_consumer.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.czf.alibaba_api.user.IUserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * @author: create by czf
 * @version: v1.0
 * @description: com.czf.alibaba_consumer.controller
 * @date: 2020-03-19 18:58
 **/
@RestController
@RequestMapping("/page")
public class PageController {

    //rpc调用方式---------------------start-------------------------
    @Reference
    IUserService userService;

    /**
     * 走自己：
     * http://localhost:8080/page/getRpc
     * 走网关
     * http://localhost:8090/sc/page/getRpc
     * @return
     */
    @GetMapping("/getRpc")
    @SentinelResource(value = "rpcc",fallback = "fallback",blockHandler = "blockHandler")
    public String getRpc() {
        return userService.getUserInfo("user:001");
    }

    public String fallback(){
        return "被限流了";
    }

    public String blockHandler(){
        return "被熔断降级了";
    }

    //rpc调用方式---------------------start-------------------------


    /**
     *
     * http://localhost:8080/page/getInfo
     *
     * http://localhost:8090/sc/page/getInfo
     *
     * @return
     */
    @CrossOrigin(origins = {"http://localhost:8100","null"})
    @GetMapping("/getInfo")
    public String getInfo() {
        return "返回结果" + new Date();
    }
}
